package com.example.easyexceldemo.service;

import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.stream.Collectors;

public class FruitInfo {
    // 模拟从数据库读取需要下载的列表信息
    public List<DownloadDTO> getFruitData() {
        List<DownloadDTO> returnList = new ArrayList<>();
        DownloadDTO d1 = new DownloadDTO();
        d1.setCategory("水果");
        d1.setFruit("苹果");
        d1.setColor("红色");
        d1.setProduceDate(new Date());

        DownloadDTO d2 = new DownloadDTO();
        BeanUtils.copyProperties(d1, d2);
        d2.setColor("绿色");

        DownloadDTO d3 = new DownloadDTO();
        BeanUtils.copyProperties(d1, d3);
        d2.setColor("白色");

        DownloadDTO t1 = new DownloadDTO();
        t1.setCategory("水果");
        t1.setFruit("香蕉");
        t1.setColor("黄色");
        t1.setProduceDate(new Date());

        DownloadDTO t2 = new DownloadDTO();
        BeanUtils.copyProperties(t1, t2);
        t2.setColor("青色");

        returnList.add(d1);
        returnList.add(d2);
        returnList.add(d3);
        returnList.add(t1);
        returnList.add(t2);
        return returnList;
    }

    // 模拟从数据库读取需要下载的水果类型
    public List<Integer> getGroupData(){
        List<Integer> groupData = new ArrayList<>();

        List<DownloadDTO> fruitData = getFruitData();

        LinkedHashMap<String, List<DownloadDTO>> fruitGroup = fruitData.stream().collect(Collectors.groupingBy(DownloadDTO::getFruit, LinkedHashMap::new, Collectors.toList()));

        for(Map.Entry<String, List<DownloadDTO>> fruitEntry :fruitGroup.entrySet()){
            int fruitSize = fruitEntry.getValue().size();
            groupData.add(fruitSize);

        }

        return groupData;
    }
}
